package zhaolu.com.example.Delicacy.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import zhaolu.com.example.Delicacy.Entity.Manager;
import zhaolu.com.example.Delicacy.MySQL.ManagerDAO;
import zhaolu.com.example.Delicacy.R;
import zhaolu.com.example.Delicacy.Utils.CommonUtils;

public class ManagerLoginActivity extends AppCompatActivity {
    private Button bt_login;
    private Button bt_register;
    private EditText et_phone;
    private EditText et_password;
    private Handler mainHandler;
    private ManagerDAO managerDAO;

    private void initView() {
        bt_login = findViewById(R.id.login_button);
        bt_register = findViewById(R.id.register_button);
        et_phone = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_password);
        managerDAO = new ManagerDAO();
        mainHandler = new Handler(getMainLooper());

        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (view.getId() == R.id.register_button) {
                    doRegister();
                }
            }
        });

        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (view.getId() == R.id.login_button) {
                    doLogin();
                }
            }
        });
    }

    private void doLogin() {
        final String phone = et_phone.getText().toString().trim();
        final String password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            CommonUtils.showShortMsg(this, "请输入用户名！");
            et_phone.requestFocus();
        } else if (TextUtils.isEmpty(password)) {
            CommonUtils.showShortMsg(this, "请输入密码！");
            et_password.requestFocus();
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Manager item = managerDAO.getManagerByUserAndPassword(phone, password);
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (item == null) {
                                CommonUtils.showShortMsg(ManagerLoginActivity.this, "用户名或密码错误！");
                            } else {
                                CommonUtils.showShortMsg(ManagerLoginActivity.this, "登陆成功！");
                            }
                        }
                    });
                }
            }).start();
        }
    }

    private void doRegister() {
        final String phone = et_phone.getText().toString().trim();
        final String password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            CommonUtils.showShortMsg(this, "请输入用户名！");
            et_phone.requestFocus();
        } else if (TextUtils.isEmpty(password)) {
            CommonUtils.showShortMsg(this, "请输入密码！");
            et_password.requestFocus();
        } else {
            Manager item = new Manager();
            item.setId(phone);
            item.setPassword(password);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final int iRow = managerDAO.addManager(item);
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            CommonUtils.showShortMsg(ManagerLoginActivity.this, "注册成功！");
                        }
                    });
                }
            }).start();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manager_login);
        initView();
    }
}